package com.qqtech.qquan.hi.controller;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.core.common.annotation.VisitAnnt;
import com.qqtech.core.common.enums.RespEnum;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.hi.model.HiHonorLevel;
import com.qqtech.qquan.hi.model.HiHonorLevelQuery;
import com.qqtech.qquan.hi.model.HiHonorLevelVo;
import com.qqtech.qquan.hi.service.HiHonorLevelService;
import com.qqtech.qquan.user.model.UserBaseVo;
import com.qqtech.qquan.user.model.UserValueVo;
import com.qqtech.qquan.user.service.UserBaseService;
import com.qqtech.qquan.user.service.UserValueService;
import com.qqtech.web.context.BaseWebController;

@Controller
@RequestMapping(value = "/hi/honor/level")
public class HiHonorLevelController extends BaseWebController<HiHonorLevel, HiHonorLevelQuery> {
	@Resource
	private HiHonorLevelService hiHonorLevelService;
	@Resource
	private UserValueService userValueService;
	@Resource
	private UserBaseService userBaseService;
    
	@Override
	protected BaseService<HiHonorLevel> getBaseService() {
		return hiHonorLevelService;
	}
	
	/**
	 * 我的等级
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/myLevel")
	@ResponseBody
	public Result myLevel() {
		Result result = new Result();
		Integer userId = getUserId();
		UserBaseVo user = userBaseService.getById(userId);
		UserValueVo userValue = userValueService.getById(userId);
		HiHonorLevelVo myLevel = null;
		HiHonorLevelQuery q = new HiHonorLevelQuery();
		q.setLevel(userValue.getHonorLevel());
		List<HiHonorLevelVo> levelList = hiHonorLevelService.queryList(q);
		if (levelList != null && !levelList.isEmpty()) {
			myLevel = levelList.get(0);
		}
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("level", userValue.getHonorLevelName());
		map.put("honorValue", userValue.getHonorValue().intValue());
		map.put("honorValueStr", "当前荣誉值"+userValue.getHonorValue().intValue());
		if (myLevel == null) {
			map.put("distance", 0);
			map.put("proportion", 0);
		} else {
			map.put("distance", myLevel.getMaximum().intValue()-userValue.getHonorValue().intValue()+1);
			BigDecimal a = new BigDecimal(myLevel.getMaximum());  
			BigDecimal b = new BigDecimal(userValue.getHonorValue()); 
			map.put("proportion", b.divide(a, 2, RoundingMode.HALF_UP).doubleValue());
		}
		map.put("nextLevel", "v"+(userValue.getHonorLevel().intValue()+1));
		map.put("nick", user.getNick());
		result.put("myLevel", map);
		return result;
	}
}
